package com.ruoyi.parkinglot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.parkinglot.service.ParkLotService;
import com.ruoyi.parkinglot.utils.Result;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.parkinglot.domain.ParkLot;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/parkLot")
public class ParkLotController{

    @Resource
    private ParkLotService parkLotService;

    /**
     * 查询停车场列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String lotName,
                       @RequestParam(required = false) String lotType) {
        PageHelper.startPage(pageNum, pageSize);
        List<ParkLot> list = parkLotService.selectParkLotList(lotName, lotType);
        PageInfo<ParkLot> pageInfo = new PageInfo<>(list);
        return Result.success(new TableDataInfo(pageInfo.getList(), (int) pageInfo.getTotal()));
    }

    /**
     * 获取停车场详细信息
     */
    @GetMapping(value = "/{lotId}")
    public Result getInfo(@PathVariable("lotId") Long lotId) {
        return Result.success(parkLotService.selectParkLotById(lotId));
    }

    /**
     * 新增停车场
     */
    @PostMapping("/Addnewparkinglot")
    public Result add(@RequestBody ParkLot parkLot) {
        return Result.success(parkLotService.insertParkLot(parkLot));
    }

    /**
     * 修改停车场
     */
    @PostMapping("/Modifyparkinglot")
    public Result edit(@RequestBody ParkLot parkLot) {
        return Result.success(parkLotService.updateParkLot(parkLot));
    }

    /**
     * 删除停车场
     */
    @PostMapping("/lotIds/{lotIds}")
    public Result remove(@PathVariable Long[] lotIds) {
        int result = parkLotService.deleteParkLotByIds(lotIds);
        return Result.success(result);
    }
}
